<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AI视频监控系统</title>
    <style>
        /* 全局样式 */
        body {
            font-family: 'Arial', sans-serif;
            line-height: 1.6;
            color: #333;
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        
        .container {
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            padding: 20px;
            margin-bottom: 20px;
        }
        
        h1, h2, h3 {
            color: #2c3e50;
        }
        
        /* 上传区域样式 */
        .upload-container {
            text-align: center;
            padding: 40px 20px;
            border: 2px dashed #ccc;
            border-radius: 5px;
            margin: 20px 0;
            transition: all 0.3s ease;
        }
        
        .upload-container:hover {
            border-color: #3498db;
        }
        
        .upload-icon {
            font-size: 48px;
            color: #3498db;
            margin-bottom: 10px;
        }
        
        /* 按钮样式 */
        .btn {
            display: inline-block;
            padding: 10px 20px;
            background-color: #3498db;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s;
        }
        
        .btn:hover {
            background-color: #2980b9;
        }
        
        /* 进度条样式 */
        .progress {
            height: 20px;
            width: 100%;
            background-color: #e0e0e0;
            border-radius: 10px;
            margin: 20px 0;
            overflow: hidden;
            position: relative;
        }
        
        .progress-bar {
            height: 100%;
            background-color: #2ecc71;
            border-radius: 10px;
            transition: width 0.3s ease;
            text-align: center;
            line-height: 20px;
            color: white;
            font-size: 14px;
        }
        
        /* 加载动画 */
        .loading {
            display: none;
            margin: 20px auto;
            text-align: center;
        }
        
        .loading-spinner {
            border: 5px solid #f3f3f3;
            border-top: 5px solid #3498db;
            border-radius: 50%;
            width: 40px;
            height: 40px;
            animation: spin 2s linear infinite;
            margin: 0 auto;
        }
        
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        
        /* 结果区域样式 */
        #results {
            display: none;
        }
        
        .result-card {
            border: 1px solid #e0e0e0;
            border-radius: 5px;
            padding: 15px;
            margin-bottom: 15px;
            background-color: white;
        }
        
        .alert {
            background-color: #ffecec;
            border-left: 4px solid #f44336;
        }
        
        .normal {
            background-color: #e8f5e9;
            border-left: 4px solid #4caf50;
        }
        
        /* 预览区域 */
        .media-preview {
            margin-top: 15px;
            display: flex;
            flex-wrap: wrap;
            gap: 15px;
        }
        
        .media-preview img, .media-preview video {
            max-width: 100%;
            border-radius: 5px;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>AI视频监控系统</h1>
        <p>上传视频文件以进行智能分析和异常检测。系统将使用人工智能技术分析视频内容，识别潜在的异常情况。</p>
    </div>
    
    <div class="container">
        <h2>上传视频</h2>
        <form id="uploadForm" enctype="multipart/form-data">
            <div class="upload-container">
                <div class="upload-icon">📁</div>
                <h3>拖放视频文件到这里</h3>
                <p>或者</p>
                <input type="file" id="videoFile" name="video" accept=".mp4,.avi,.mov,.mkv" style="display:none;">
                <button type="button" class="btn" id="browseBtn">浏览文件</button>
                <p id="selectedFile">尚未选择文件</p>
            </div>
            
            <div id="uploadProgress" class="progress" style="display:none;">
                <div class="progress-bar" style="width: 0%">0%</div>
            </div>
            
            <button type="submit" class="btn" id="uploadBtn" disabled>开始分析</button>
        </form>
        
        <div id="loading" class="loading">
            <div class="loading-spinner"></div>
            <p>正在分析视频，这可能需要几分钟时间...</p>
        </div>
    </div>
    
    <div id="results" class="container">
        <h2>分析结果</h2>
        <div id="summary"></div>
        <div id="segmentResults"></div>
    </div>
    
    <script>
        // 文件选择处理
        const browseBtn = document.getElementById('browseBtn');
        const videoFile = document.getElementById('videoFile');
        const selectedFile = document.getElementById('selectedFile');
        const uploadBtn = document.getElementById('uploadBtn');
        const uploadForm = document.getElementById('uploadForm');
        const uploadProgress = document.getElementById('uploadProgress');
        const progressBar = document.querySelector('.progress-bar');
        const loading = document.getElementById('loading');
        const results = document.getElementById('results');
        const summary = document.getElementById('summary');
        const segmentResults = document.getElementById('segmentResults');
        
        // 点击浏览按钮触发文件选择
        browseBtn.addEventListener('click', () => {
            videoFile.click();
        });
        
        // 文件选择变化处理
        videoFile.addEventListener('change', (e) => {
            if (e.target.files.length > 0) {
                const file = e.target.files[0];
                selectedFile.textContent = `已选择: ${file.name} (${formatFileSize(file.size)})`;
                uploadBtn.disabled = false;
            } else {
                selectedFile.textContent = '尚未选择文件';
                uploadBtn.disabled = true;
            }
        });
        
        // 拖放功能
        const uploadContainer = document.querySelector('.upload-container');
        
        uploadContainer.addEventListener('dragover', (e) => {
            e.preventDefault();
            uploadContainer.style.borderColor = '#3498db';
            uploadContainer.style.backgroundColor = '#f0f9ff';
        });
        
        uploadContainer.addEventListener('dragleave', () => {
            uploadContainer.style.borderColor = '#ccc';
            uploadContainer.style.backgroundColor = '';
        });
        
        uploadContainer.addEventListener('drop', (e) => {
            e.preventDefault();
            uploadContainer.style.borderColor = '#ccc';
            uploadContainer.style.backgroundColor = '';
            
            if (e.dataTransfer.files.length > 0) {
                videoFile.files = e.dataTransfer.files;
                const file = e.dataTransfer.files[0];
                selectedFile.textContent = `已选择: ${file.name} (${formatFileSize(file.size)})`;
                uploadBtn.disabled = false;
            }
        });
        
        // 文件大小格式化
        function formatFileSize(bytes) {
            if (bytes < 1024) return bytes + ' B';
            else if (bytes < 1048576) return (bytes / 1024).toFixed(2) + ' KB';
            else if (bytes < 1073741824) return (bytes / 1048576).toFixed(2) + ' MB';
            else return (bytes / 1073741824).toFixed(2) + ' GB';
        }
        
        // 处理表单提交
        uploadForm.addEventListener('submit', async (e) => {
            e.preventDefault();
            
            if (!videoFile.files[0]) {
                alert('请先选择一个视频文件');
                return;
            }
            
            const formData = new FormData();
            formData.append('video', videoFile.files[0]);
            
            // 显示上传进度条
            uploadProgress.style.display = 'block';
            uploadBtn.disabled = true;
            browseBtn.disabled = true;
            
            try {
                // 开始上传
                const xhr = new XMLHttpRequest();
                
                xhr.upload.addEventListener('progress', (e) => {
                    if (e.lengthComputable) {
                        const percent = Math.round((e.loaded / e.total) * 100);
                        progressBar.style.width = percent + '%';
                        progressBar.textContent = percent + '%';
                    }
                });
                
                xhr.addEventListener('load', function() {
                    // 上传完成，显示加载动画
                    uploadProgress.style.display = 'none';
                    loading.style.display = 'block';
                    
                    if (xhr.status === 200) {
                        // 处理服务器响应
                        const response = JSON.parse(xhr.responseText);
                        displayResults(response);
                    } else {
                        alert('上传失败: ' + xhr.statusText);
                        uploadBtn.disabled = false;
                        browseBtn.disabled = false;
                        loading.style.display = 'none';
                    }
                });
                
                xhr.addEventListener('error', function() {
                    alert('上传过程中发生错误');
                    uploadBtn.disabled = false;
                    browseBtn.disabled = false;
                    uploadProgress.style.display = 'none';
                    loading.style.display = 'none';
                });
                
                xhr.open('POST', '/upload', true);
                xhr.send(formData);
                
            } catch (error) {
                alert('上传失败: ' + error.message);
                uploadBtn.disabled = false;
                browseBtn.disabled = false;
                uploadProgress.style.display = 'none';
                loading.style.display = 'none';
            }
        });
        
        // 显示分析结果
        function displayResults(data) {
            loading.style.display = 'none';
            results.style.display = 'block';
            
            // 显示结果摘要
            summary.innerHTML = `
                <h3>视频: ${data.filename}</h3>
                <p>${data.summary}</p>
                <p>共分析了 ${data.segments.length} 个视频片段</p>
            `;
            
            // 清空之前的结果
            segmentResults.innerHTML = '';
            
            // 显示每个片段的结果
            data.segments.forEach(segment => {
                const segmentCard = document.createElement('div');
                segmentCard.className = `result-card ${segment.has_alert ? 'alert' : 'normal'}`;
                
                let cardContent = `
                    <h3>片段 #${segment.segment_id}</h3>
                    <p>时间范围: ${segment.start_time} 至 ${segment.end_time}</p>
                `;
                
                if (segment.has_alert) {
                    cardContent += `
                        <div>${segment.alert}</div>
                        <div>${segment.description}</div>
                        <div class="media-preview">
                    `;
                    
                    if (segment.image_file) {
                        cardContent += `<img src="/video_warning/${segment.image_file}" alt="Warning Screenshot">`;
                    }
                    
                    if (segment.video_file) {
                        cardContent += `
                            <video controls>
                                <source src="/video_warning/${segment.video_file}" type="video/mp4">
                                Your browser does not support the video tag.
                            </video>
                        `;
                    }
                    
                    cardContent += `</div>`;
                } else {
                    cardContent += `<p>Normal segment - No anomalies detected</p>`;
                }
                
                segmentCard.innerHTML = cardContent;
                segmentResults.appendChild(segmentCard);
            });
            
            // 重置表单
            uploadBtn.disabled = false;
            browseBtn.disabled = false;
            videoFile.value = '';
            selectedFile.textContent = '尚未选择文件';
        }
    </script>
</body>
</html>
